package cn.maydaymemory.mastery.message.util;

import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.List;

public class PageMessage implements Cloneable{
    private List<String> messages;
    private CommandSender viewer;
    private String title;
    private String footer;
    private int stepLength;
    private boolean indexed;
    public PageMessage(List<String> messages){
        this.messages=new ArrayList<>();
        this.messages.addAll(messages);
        stepLength = 10;
        indexed = false;
    }
    public PageMessage clone(){
        try {
            return (PageMessage) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * @param index 需要删除的的信息的下标
     * @return 返回当前PageMessage实例
     */
    public PageMessage removeMessage(int index){
        messages.remove(index);
        return this;
    }
    /**
     * @param msg 需要添加的信息
     * @return 返回当前PageMessage实例
     */
    public PageMessage addMessage(String msg){
        messages.add(msg);
        return this;
    }
    /**
     * @return 返回当前的所有信息
     */
    public List<String> messages(){
        return messages;
    }
    /**
     * @param messages 设置当前的所有信息
     * @return 返回当前PageMessage实例
     */
    public PageMessage messages(List<String> messages){
        this.messages = messages;
        return this;
    }

    /**
     * @return 返回当前的信息接收者
     */
    public CommandSender viewer(){
        return viewer;
    }
    /**
     * @param viewer 设置当前的信息接收者
     * @return 返回当前PageMessage实例
     */
    public PageMessage viewer(CommandSender viewer){
        this.viewer=viewer;
        return this;
    }

    /**
     * @return 返回当前每页的标题
     */
    public String title(){
        return title;
    }
    /**
     * @param title 设置当前每页的标题
     * @return 返回当前PageMessage实例
     */
    public PageMessage title(String title) {
        this.title=title;
        footer= EdgingUtil.getFooter("§b1§f/2", StringUtil.getGBKLengthWithoutColorCode(title));
        footer=footer.replaceAll("1","%current_page%").replaceAll("2","%total_page%");
        return this;
    }

    /**
     * @return 返回当前每页最多显示行数
     */
    public int stepLength(){
        return stepLength;
    }

    /**
     * @param stepLength 设置当前每页最多显示行数
     * @return 返回当前PageMessage实例
     */
    public PageMessage stepLength(int stepLength){
        if(stepLength>0){
            this.stepLength = stepLength;
        }else {
            this.stepLength = 10;
        }
        return this;
    }

    /**
     * @return 返回是否显示行数
     */
    public boolean indexed(){
        return indexed;
    }

    /**
     * @param indexed 设置是否显示行数
     * @return 返回当前PageMessage实例
     */
    public PageMessage indexed(boolean indexed){
        this.indexed = indexed;
        return this;
    }

    /**
     * 向设置的viewer发送指定页数的信息。
     * @param page 要发送的页码
     */
    public void sendMessages(int page){
        if(messages==null||viewer==null) return;
        int alpha = stepLength()*(page-1);
        if(alpha>=messages.size()) return;
        if(title!=null) viewer.sendMessage(title.replaceAll("%current_page%",""+page).replaceAll("%total_page%",""+(messages.size()%stepLength==0?messages.size()/stepLength:messages.size()/stepLength+1) ));
        for(int f= alpha;f<alpha+stepLength()&&f<messages.size();f++){
            if(indexed) viewer.sendMessage((f+1)+"."+messages.get(f));
            else viewer.sendMessage(messages.get(f));
        }
        if(footer!=null) viewer.sendMessage(footer.replaceAll("%current_page%",""+page).replaceAll("%total_page%",""+(messages.size()%stepLength==0?messages.size()/stepLength:messages.size()/stepLength+1) ));
    }
}
